package com.egg.ylt.presenter.impl;

import android.app.Activity;
import android.net.Uri;

import com.alibaba.fastjson.JSONObject;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.UploadPhotoEntity;
import com.egg.ylt.uploadingphoto.UriUtils;
import com.egg.ylt.view.IWriteAndChosePhotosView;
import com.google.gson.Gson;
import com.lzy.imagepicker.bean.ImageItem;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By Dlx
 * on 2019/2/27
 */
public class WriteAndChosePhotosPresenter extends BasePresenter<IWriteAndChosePhotosView> {

    private int picCounts = 0;
    private List<String> imageUrls = new ArrayList<>(10);
    private List<String> thumbnailUrls = new ArrayList<>(10);

    public void submitChosenData(List<String> imageUrls, List<String> thumbnailUrls, String content) {
        mView.showDialogLoading("正在发布");
        HashMap<String, String> parms = new HashMap<>();
        parms.put("token", Constants.TOKEN);
        parms.put("companyId", Constants.COMPANYID);
        parms.put("babyId", Constants.BABYID);
        parms.put("content", content);
        Gson gson = new Gson();
        parms.put("imageUrl", gson.toJson(imageUrls));
        parms.put("thumbnailUrl", gson.toJson(thumbnailUrls));
//        Map<String, List<String>> parmsWithList = new HashMap<>();
//        parmsWithList.put("imageUrl", imageUrls);
//        parmsWithList.put("thumbnailUrl", thumbnailUrls);
        String jsonStr = JSONObject.toJSONString(parms);
        RequestManager.getInstance().requestPostByAsyn(API.POST_SAVE_DIARY_PHOTO, parms, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.onSubmitSucceed();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showToast(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showToast(error.getMsg());
            }
        });
    }

    public void uploadPhotos(final List<ImageItem> imagePathList) {

        mView.dismissDialogLoading();
        if (imagePathList.size() > 0) {
            picCounts++;
            mView.showDialogLoading("图片上传中……当前第" + picCounts + "张");
            HashMap<String, Object> hashMap = new HashMap<>();
            File photoFile = new File(imagePathList.get(0).path);
            hashMap.put("file", photoFile);
            RequestManager.getInstance().upLoadFile(API.POST_UPLOAD_PHOTO, hashMap, new ReqCallBack<UploadPhotoEntity>() {

                @Override
                public void onReqSuccess(UploadPhotoEntity result) {
                    imageUrls.add(result.getShowImg());
                    thumbnailUrls.add(result.getCompressImg());
                    imagePathList.remove(0);
                    uploadPhotos(imagePathList);
                }

                @Override
                public void onFailure(String result) {
                    mView.showToast(result);
                    picCounts = 0;
                    mView.dismissDialogLoading();
                }

                @Override
                public void onReqFailed(ErrorBean error) {
                    mView.showToast(error.getMsg());
                    picCounts = 0;
                    mView.dismissDialogLoading();
                }
            });

        } else {
            picCounts = 0;
            mView.onPhotosUploadSucceed(imageUrls, thumbnailUrls);
        }
    }

}
